package com.qf.controller;

import com.qf.entity.Address;
import com.qf.entity.User;
import com.qf.service.AddressService;
import com.qf.service.impl.AddressServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/18 22:58
 * @Version 1.0
 */

@WebServlet("/address")
public class AddressController  extends BaseController {
    private AddressService service=new AddressServiceImpl();
    /**
     * 查询当前用户下的地址列表，转发到self_info.jsp进行展示
     * @param request
     * @param response
     * @return
     */
    public String getAddress(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        List<Address> addressList = service.getAddressList(user.getId());
        request.setAttribute("addList",addressList);
        return "forward:self_info.jsp";
    }
    //设置地址默认值
    public String setDefault(HttpServletRequest request, HttpServletResponse response){
        String strId = request.getParameter("id");
        User user = (User) request.getSession().getAttribute("loginUser");
        int result=service.setDefault(Integer.parseInt(strId),user.getId());
        if(result==0){
            request.setAttribute("msg","设置失败");
            return "forward:error/error.jsp";
        }else{
            return "redirect:address?action=getAddress";
        }

    }
}